//
//  CompetitionListViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/8.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "CompetitionListViewController.h"
#import "CompetitionTableVCell.h"

@interface CompetitionListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    UITableView * _tableView;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
     NSDictionary * userDic;
}


@end

@implementation CompetitionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"活动排名")
    
    _page = 1;
    
    _dataArray = @[].mutableCopy;
    
    [self getActivityListData];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self creatTableView];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[CompetitionTableVCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[CompetitionTableVCell reuseIdentifier]];
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        [self getActivityListData];
        
        
    }];
    _tableView.mj_header = header;
    
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getActivityListData];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if ([userDic[@"if_show"] isEqualToString:@"1"]) {
        return 90;
    }else {
        
        return 0.01;
    }

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    CompetitionTableVCell  * cell = [tableView dequeueReusableCellWithIdentifier:[CompetitionTableVCell reuseIdentifier] forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.userIcon sd_setImageWithURL:ImageUrl(dic[@"avatar"]) placeholderImage:ImageName(@"img_user_default")];
    cell.nameLabel.text = [NSString stringWithFormat:@"Top:%@  %@",dic[@"rank"],dic[@"user_name"]];
    cell.pointLabel.text = [NSString stringWithFormat:@"%@积分",dic[@"vote_number"]];
    
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    

    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = Color(106, 199, 242);
    label.font = [UIFont systemFontOfSize:14];
    label.text =@"温馨提示：当前排名为实时排名";
    [view addSubview:label];
    
    return view;

}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
    float height;
    
    
    if ([userDic[@"if_show"] isEqualToString:@"1"]) {
        height =  90;

    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 90)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIImageView * userIcon = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 60, 60)];
    userIcon.layer.cornerRadius = 30.f;
    userIcon.layer.masksToBounds = YES;
    [userIcon  sd_setImageWithURL:ImageUrl(userDic[@"avatar"]) placeholderImage:ImageName(@"img_user_default")];
    [view addSubview:userIcon];
    
    UIImageView * pointImage = [[UIImageView alloc]initWithFrame:CGRectMake(78, 50, 25, 25)];
    pointImage.image = ImageName(@"icon_activity_integral");
    [view addSubview:pointImage];
    
    UILabel * pointlabel =[[UILabel alloc]initWithFrame:CGRectMake(105, 52, WIDTH - 130, 20)];
    pointlabel.font = [UIFont systemFontOfSize:14];
    pointlabel.text =[NSString stringWithFormat:@"%@积分",userDic[@"vote_number"]];
    pointlabel.textColor = [UIColor redColor];
    [view addSubview:pointlabel];
    
    UILabel * label =[[UILabel alloc]initWithFrame:CGRectMake(80, 15, WIDTH - 90, 20)];
    label.font = [UIFont systemFontOfSize:15];
    label.text =[NSString stringWithFormat:@"Top:%@  %@",userDic[@"rank"],@"本人"];
    [view addSubview:label];
    
    return view;
    }else {
    
        return nil;
    }


}


-(void)getActivityListData{
    
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"activity_id":self.activity_id,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token")
                           };
    
    //    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_activityTopList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage =  [responseObject[@"datas"][@"total_page"] intValue];
            
            userDic = responseObject[@"datas"][@"user_info"];
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"list"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"list"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    //                    noBodyView.hidden =YES;
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;
                    //                    noBodyView.hidden =NO;
                    //
                    //                    [self.view addSubview:noBodyView];
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
}



@end
